<?php

class fop_Object_Table {

  protected $_name;
  protected $_description;
  protected $_creatorEmail;
  protected $_creationDate;
  
  /* definition of the nonce table */
  static protected $_dbTableTableName = 'tables';
  static protected $_dbTableNameCol = 'name';
  static protected $_dbTableDescriptionCol = 'description';
  static protected $_dbTableCreatorEmailCol = 'creatorEmail';
  static protected $_dbTableCreationdateCol = 'creationDate';  

  public function __construct($name,$data = array()) {
  	if(empty($data)) {
      //We validate the name
  	  self::_validateName($name);
  	  //We get the data from the database
      $db = Zend_Registry::get('dt_dbs')->getDb('fop','table','key');
      $select = $db->select()->from(self::$_dbTableTableName);
      $select->where($db->quoteInto(self::$_dbTableNameCol ." = ?", $name));
      $res = $db->fetchAll($select->__toString());

      if(empty($res)) {
  	    require_once('fop/objects/Exception.php');
  	    throw new fop_Object_Exception('Table not found',6); 
      }
      $data = $res[0];
  	}

    $this->_name = $data[self::$_dbTableNameCol];
    $this->_description = $data[self::$_dbTableDescriptionCol];
    $this->_creatorEmail = $data[self::$_dbTableCreatorEmailCol];
    $this->_creationDate = $data[self::$_dbTableCreationdateCol];	

      	
  }	//end constructor
  
  public function getName() {
    return $this->_name;	
  }
  
  public function asArray() {
  	$results = array();
    $results['name'] = $this->_name;
    $results['description'] = $this->_description;
    $results['creatorEmail'] = $this->_creatorEmail;
    $results['creationDate'] = $this->_creationDate;	  	
    return $results;
  }
	
  static public function listing() {
    $db = Zend_Registry::get('dt_dbs')->getDb('fop','table','key');
    $select = $db->select()->from(self::$_dbTableTableName);
    $res = $db->fetchAll($select->__toString());  

    if(empty($res)) return array();

    //We must reinterpret the rows so it is independant to the way it is stored
    $results = array();
    foreach($res as $num => $row) {
    	$table = new fop_Object_Table('',$row);
    	$results[] = $table->asArray();	
    }
    
    return $results;
      
  } //end list
  
  
  
  static public function create($params) {
  	//check if mandatory parameters are provided
  	if(empty($params['name']) || empty($params['description'])) {
  	  require_once('fop/objects/Exception.php');
  	  throw new fop_Object_Exception('Mandatory data is missing',1);
  	} //end empty parameters
  	
  	//We clean the data provided
  	//We validate the name
  	self::_validateName($params['name']);
  	$name = strToUpper($params['name']);
  	
  	//We clean the description, only constraint is that it should be valid HTML
	require_once('htmlpurifier/library/HTMLPurifier.auto.php');
  	$purifier = new HTMLPurifier();
    $description = $purifier->purify($params['description']);

    
    //We check if the Table is not already existing
    $existing = true;
    try{
    	$table = new fop_Object_Table($name);
    } catch(fop_Object_Exception $e) {
    	$existing = false;	
    }
    if($existing) {
  	  require_once('fop/objects/Exception.php');
  	  throw new fop_Object_Exception('Table is already existing',5);    	
    }

    //OK we can create the table
	$email = Zend_Registry::get('app_user')->getEmail();
  	$creationDate = time();
  	
  	$data = array(
      self::$_dbTableNameCol => $name,
      self::$_dbTableDescriptionCol => $description,
      self::$_dbTableCreatorEmailCol => $email,
      self::$_dbTableCreationdateCol => $creationDate  	
  	);
    $db = Zend_Registry::get('dt_dbs')->getDb('fop','table','key');
    $db->insert(self::$_dbTableTableName, $data); 	
  	return true;
  	
  } //end create

  static protected function	_validateName($name) {
    	//table name can only be lowercase alphabet characters and less or equal 32 characters
  	if(preg_match('/[^a-zA-Z0-9]/',$name)) {
  	  require_once('fop/objects/Exception.php');
  	  throw new fop_Object_Exception('table name can only be alphabet lower case characters or numerical',2);  		
  	}
  	if(32 < strlen($name)) {
  	  require_once('fop/objects/Exception.php');
  	  throw new fop_Object_Exception('table name must be less than 33 characters',3);
  	}
  	return true;	
  }
}